package com.china_goldcard.choosedialog;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by jk on 16/10/31.
 */

public class ChooseDialogAdapter extends RecyclerView.Adapter<ChooseDialogVH> {

    private List<ChooseDialogItem> data;
    private Context context;
    private int itemH;
    private int selectIcon;

    public interface OnClickListener{
        void onItemClick(ChooseDialogItem item);
    }
    private OnClickListener onClickListener;

    public OnClickListener getOnClickListener() {
        return onClickListener;
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public void setItemH(int itemH) {
        this.itemH = itemH;
    }

    public ChooseDialogAdapter(Context context, List<ChooseDialogItem> data){
        this.context = context;
        this.data = data;
    }

    @Override
    public ChooseDialogVH onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.choose_dialog_item,parent,false);
        itemView.getLayoutParams().height = itemH;
        return new ChooseDialogVH(itemView);
    }

    @Override
    public void onBindViewHolder(ChooseDialogVH holder, int position) {
        final ChooseDialogItem model = data.get(position);
        holder.textView.setText(model.getTitle());
        if (selectIcon!=0){
            holder.imageView.setImageResource(selectIcon);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListener!=null){
                    onClickListener.onItemClick(model);
                }
            }
        });
        if (position == data.size()-1){
            holder.line.setVisibility(View.GONE);
        }else{
            holder.line.setVisibility(View.VISIBLE);
        }
    }

    public void setSelectIcon(int selectIcon) {
        this.selectIcon = selectIcon;
    }

    @Override
    public int getItemCount() {
        return this.data.size();
    }
}

class ChooseDialogVH extends RecyclerView.ViewHolder{

    TextView textView;

    ImageView imageView;

    View line;

    public ChooseDialogVH(View itemView) {
        super(itemView);
        textView = (TextView)itemView.findViewById(R.id.choose_dialog_item_title_tv);
        imageView = (ImageView)itemView.findViewById(R.id.choose_dialog_item_selected_icon);
        line = (View)itemView.findViewById(R.id.choose_dialog_item_line);
    }
}
